package Master;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.HttpJspPage;
import javax.sound.midi.MetaEventListener;

import login.LoginForm;
import login.UserModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import Candidate.CandidateForm;
import Eclass.EclassForm;
import Eclass.Question.QuestionModel;
import Eclass.Score.ScoreForm;
import Forum.ForumThreadForm;

public class SchoolAction extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SchoolForm schoolForm = (SchoolForm)form;
		SchoolManager manager = new SchoolManager();		
		HttpSession session = request.getSession(true);
		UserModel user = (UserModel) session.getAttribute("userLogin");
		boolean result;
		String resultText = "";
		System.out.println("Task : " + schoolForm.getTask());
		
//-------------------------------------------------------------- Navigation ----------------------------------------------------------
		CandidateForm candidateForm = new CandidateForm();
		//SchoolForm schoolForm = new SchoolForm();
		EclassForm eClassForm = new EclassForm();
		ForumThreadForm forumForm = new ForumThreadForm();
		ScoreForm scoreForm = new ScoreForm();
		
		if("updateCandidate".equalsIgnoreCase(schoolForm.getTask())) {
			candidateForm.getcModel().setRegisNo("01-13-12- 029");
			candidateForm.setTask("update");
			return mapping.findForward("candidate");
		}else if("manageTrainee".equalsIgnoreCase(schoolForm.getTask())) {	
			eClassForm.setTask("manageTrainee");
			return mapping.findForward("eclass");
		}else if("batch".equalsIgnoreCase(schoolForm.getTask())) {
			eClassForm.setTask("batch");
			return mapping.findForward("eclass");
		}else if("listExamPrep".equalsIgnoreCase(schoolForm.getTask())) {
			eClassForm.setTask("listExamPrep");
			return mapping.findForward("eclass");
//				}else if("manageSchool".equalsIgnoreCase(schoolForm.getTask())) {
//					schoolForm.setTask("manageSchool");
//					return mapping.findForward("master");
  		}else if("forumPage".equalsIgnoreCase(schoolForm.getTask())) {
			forumForm.setTask("forumPage");
			return mapping.findForward("forum");
		}else if("report".equalsIgnoreCase(schoolForm.getTask())) {
			scoreForm.setTask("report");
			return mapping.findForward("score");
		}
//------------------------------------------------------------------------------------------------------------------------------
		
	//========= Manage School =========
		if ("manageSchool".equalsIgnoreCase(schoolForm.getTask())) {	
			schoolForm.setCityList(manager.selectAllCity(null));
			schoolForm.setSchoolList(manager.selectAllSchool(schoolForm.getParamSchool(),schoolForm.getParamCity()));
			return mapping.findForward("manageSchool");
		}else if ("insertSchool".equalsIgnoreCase(schoolForm.getTask())) {
			//System.out.println("Name : " + schoolForm.getSchoolModel().getSchoolName());
			//System.out.println("City ID : " + schoolForm.getSchoolModel().getCityId());
			result = manager.InsertSchool(schoolForm.getSchoolModel());
			setResult(result, "Add new school", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(null));
			schoolForm.setSchoolList(manager.selectAllSchool(schoolForm.getParamSchool(),schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageSchool");
		}else if("updateSchool".equalsIgnoreCase(schoolForm.getTask())){
			//System.out.println("ID : " + schoolForm.getSchoolModel().getSchoolId());
			//System.out.println("Name : " + schoolForm.getSchoolModel().getSchoolName());
			//System.out.println("City ID : " + schoolForm.getSchoolModel().getCityId());
			result = manager.UpdateSchool(schoolForm.getSchoolModel());
			setResult(result, "Update school", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(null));
			schoolForm.setSchoolList(manager.selectAllSchool(schoolForm.getParamSchool(),schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageSchool");
		}else if ("deleteSchool".equalsIgnoreCase(schoolForm.getTask())) {
			//System.out.println("ID : " + schoolForm.getSchoolModel().getSchoolId());
			result = manager.DeleteSchool(schoolForm.getSchoolModel());
			setResult(result, "Delete school", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(null));
			schoolForm.setSchoolList(manager.selectAllSchool(schoolForm.getParamSchool(),schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageSchool");
		}else 
		
	//========== Manage City ==============
		if ("insertCity".equalsIgnoreCase(schoolForm.getTask())) {
			//System.out.println("ID : " + schoolForm.getSchoolModel().getCityId());
			//System.out.println("Name : " + schoolForm.getSchoolModel().getCityName());
			result = manager.InsertCity(schoolForm.getSchoolModel());
			setResult(result, "Add new city", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageCity");		
		}else if("updateCity".equalsIgnoreCase(schoolForm.getTask())){
			//System.out.println("ID : " + schoolForm.getSchoolModel().getCityId());
			//System.out.println("Name : " + schoolForm.getSchoolModel().getCityName());
			result = manager.UpdateCity(schoolForm.getSchoolModel());
			setResult(result, "Update city", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageCity");
		}else if ("deleteCity".equalsIgnoreCase(schoolForm.getTask())) {
			//System.out.println("ID : " + schoolForm.getSchoolModel().getCityId());
			//System.out.println("Name : " + schoolForm.getSchoolModel().getCityName());
			result = manager.DeleteCity(schoolForm.getSchoolModel());
			setResult(result, "Delete city", session, schoolForm.getTask());
			schoolForm.setCityList(manager.selectAllCity(schoolForm.getParamCity()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageCity");
		}
		
	//===========Manage Job ===============
		else if ("manageJob".equalsIgnoreCase(schoolForm.getTask())) {	
			
			schoolForm.setJobList(manager.selectAllJob(schoolForm.getParamJobId(),schoolForm.getParamDescription()));
			return mapping.findForward("manageJob");
		}
		else if ("insertJob".equalsIgnoreCase(schoolForm.getTask())) {
			

			System.out.println("Job id xxxxxxxxxxxxxxx "+schoolForm.getSchoolModel().getDescription());
			boolean flag = false;
			flag=manager.InsertJob(schoolForm.getSchoolModel());
//			if(flag==true){
//				System.out.println("Benar keinsert");
//			}
			
			setResult(flag, "Add new job", session, schoolForm.getTask());
			schoolForm.setJobList(manager.selectAllJob(schoolForm.getParamJobId(),schoolForm.getParamDescription()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageJob");
		}
		else if ("updateJob".equalsIgnoreCase(schoolForm.getTask())) {
			boolean flag = false;
			flag=manager.UpdateJob(schoolForm.getSchoolModel());
//			if(flag==true){
//				System.out.println("Benar keUpdate");
//			}
			setResult(flag, "Update job", session, schoolForm.getTask());
			schoolForm.setJobList(manager.selectAllJob(schoolForm.getParamJobId(),schoolForm.getParamDescription()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageJob");
		}		
		else if ("deleteJob".equalsIgnoreCase(schoolForm.getTask())) {
			result = manager.DeleteJob(schoolForm.getSchoolModel());
			setResult(result, "Delete job", session, schoolForm.getTask());
			schoolForm.setJobList(manager.selectAllJob(schoolForm.getParamJobId(),schoolForm.getParamDescription()));
			//schoolForm.setTask(null);
			return mapping.findForward("manageJob");
		}
		
	//=========== Manage Group Class ============
		else if("manageGroupClass".equalsIgnoreCase(schoolForm.getTask())){
			schoolForm.setGroupClassList(manager.selectAllGroupClass(schoolForm.getParamClassName()));
			return mapping.findForward("manageGroupClass");
		}else if("insertGroupClass".equalsIgnoreCase(schoolForm.getTask())){
			schoolForm.setClassList(manager.selectAllClass());
			
			//Rere 23 Des 2013
			List list = new ArrayList();			
			GroupClassModel gcModel = new GroupClassModel();
			gcModel.setGroupClassId(0);
			gcModel.setGrClassDetailWeight(0);
			list.add(gcModel);
			
			schoolForm.setGroupClassList(list);
			
			return mapping.findForward("insertGroupClass");
		}else if("insertGroupClassExe".equalsIgnoreCase(schoolForm.getTask())){
//			result = manager.insertGroupClass(schoolForm.getGroupClassName(), schoolForm.getGroupClassWeight(), user.getUserid());
//			if(result){
//				GroupClassModel gClassModel = manager.getLastGroupClassId();
//				Integer grClassId = gClassModel.getGroupClassId();
//				System.out.println(grClassId);
//				for (int i = 0; i < schoolForm.getClassId().length; i++) {
//					if(schoolForm.getClassId()[i]!=0){
//						//result = manager.insertGroupClassDetail(grClassId, schoolForm.getClassId()[i], schoolForm.getGrClassDetailWeight()[i], user.getUserid());
//						setResult(result, "Add new group class", session, schoolForm.getTask());
//					}
//				}
//			}
//			schoolForm.setGroupClassList(manager.selectAllGroupClass());
//			return mapping.findForward("manageGroupClass");
			
			//Rere 24 Des 2013
			//set model from form
			GroupClassModel gcModel = new GroupClassModel();
			gcModel.setGroupClassId(schoolForm.getGroupClassId());
			gcModel.setGroupClassName(schoolForm.getGroupClassName());
			gcModel.setGroupClassWeight(schoolForm.getGroupClassWeight());
			gcModel.setUsrupd(user.getUserid());
			
			result = manager.insertGroupClass(gcModel);		
			
			System.out.println("Group Class ID : " + gcModel.getGroupClassId());
			System.out.println("Group Class Name : " + gcModel.getGroupClassName());
			System.out.println("Group Class Weight : " + gcModel.getGroupClassWeight());
			
			//insert detail
			GroupClassModel detailModel;
			for(int i=0; i<schoolForm.getClassId().length; i++){
				//set detail model
				detailModel = new GroupClassModel();
				detailModel.setGroupClassId(gcModel.getGroupClassId());
				detailModel.setClassId(schoolForm.getClassId()[i]);
				detailModel.setGrClassDetailWeight(schoolForm.getGrClassDetailWeight()[i]);
				
				System.out.println();
				System.out.println("Class ID : " + schoolForm.getClassId()[i]);
				System.out.println("Detail Class Weight : " + schoolForm.getGrClassDetailWeight()[i]);
				
				result = manager.insertGroupClassDetail(detailModel);
			}
			
			this.setResult(true, "Update Group Class", session, schoolForm.getTask());
			
			schoolForm.setGroupClassList(manager.selectAllGroupClass(schoolForm.getParamClassName()));
			return mapping.findForward("manageGroupClass");
		}else if("groupClassDetail".equalsIgnoreCase(schoolForm.getTask())){
			List detailList = manager.getGroupClassDetail(schoolForm.getGroupClassId());
			response.setContentType("text/text;charset=utf-8");
			response.setHeader("cache-control", "no-cache");
			PrintWriter out = response.getWriter();
			String append = null;
			append = "<table width='80%' class='boxShadow' border='0'>" + 
					"<tr class='headerBox formHeaderBackground'>" +
					"<th class='formListStyle'>Subject of Group Class</th>" +
					"<th class='formListStyle'>Weight</th>" +
				  "</tr>";
			for (int i = 0; i < detailList.size(); i++) {
				GroupClassModel grcClassModel = (GroupClassModel) detailList.get(i);
				append += "<tr class='formListBackground'>" +
							"<td class='formListStyle'>"+grcClassModel.getClassName()+"</td>" +
							"<td class='formListStyle' align='center'>"+grcClassModel.getGrClassDetailWeight()+"</td>" +
						   "</tr>";
			}
			append += "</table>";
			out.println(append);
			out.flush();
			return null;
		}else if("deleteGroupClass".equalsIgnoreCase(schoolForm.getTask())){
			result = manager.deleteGroupClass(schoolForm.getGroupClassId());
			setResult(result, "Delete Group Class", session, schoolForm.getTask());
			schoolForm.setGroupClassList(manager.selectAllGroupClass(""));
			return mapping.findForward("manageGroupClass");
		}
	
	//Rere 23 Des 2013
		else if("updateGroupClass".equalsIgnoreCase(schoolForm.getTask())){	
			System.out.println("Group Class ID : " + schoolForm.getGroupClassId());
			GroupClassModel gcModel = manager.getGroupClassById(schoolForm.getGroupClassId());
			List detail = manager.getGroupClassDetailByClass(schoolForm.getGroupClassId());
			//System.out.println("Detail Length : " + detail.size());
			
			//set form detail
			schoolForm.setGroupClassId(gcModel.getGroupClassId());
			schoolForm.setGroupClassName(gcModel.getGroupClassName());
			schoolForm.setGroupClassWeight(gcModel.getGroupClassWeight());
			schoolForm.setGroupClassList(detail);
			
			schoolForm.setClassList(manager.selectAllClass());
			return mapping.findForward("insertGroupClass");
		}else if("updateGroupClassExe".equalsIgnoreCase(schoolForm.getTask())){
			//set model from form
			GroupClassModel gcModel = new GroupClassModel();
			gcModel.setGroupClassId(schoolForm.getGroupClassId());
			gcModel.setGroupClassName(schoolForm.getGroupClassName());
			gcModel.setGroupClassWeight(schoolForm.getGroupClassWeight());
			gcModel.setUsrupd(user.getUserid());
			
			result = manager.updateGroupClass(gcModel);		
			
//			System.out.println("Group Class ID : " + gcModel.getGroupClassId());
//			System.out.println("Group Class Name : " + gcModel.getGroupClassName());
//			System.out.println("Group Class Weight : " + gcModel.getGroupClassWeight());
			
			//update detail
			result = manager.DeleteGroupClassDetail(gcModel.getGroupClassId());
			GroupClassModel detailModel;
			for(int i=0; i<schoolForm.getClassId().length; i++){
				//set detail model
				detailModel = new GroupClassModel();
				detailModel.setGroupClassId(gcModel.getGroupClassId());
				detailModel.setClassId(schoolForm.getClassId()[i]);
				detailModel.setGrClassDetailWeight(schoolForm.getGrClassDetailWeight()[i]);
				detailModel.setUsrupd(user.getUserid());
				
//				System.out.println();
//				System.out.println("Class ID : " + schoolForm.getClassId()[i]);
//				System.out.println("Detail Class Weight : " + schoolForm.getGrClassDetailWeight()[i]);
				
				result = manager.insertGroupClassDetail(detailModel);
			}
			
			this.setResult(true, "Update Group Class", session, schoolForm.getTask());
			
			schoolForm.setGroupClassList(manager.selectAllGroupClass(schoolForm.getParamClassName()));
			return mapping.findForward("manageGroupClass");
		}
		
	//default -> Manage City	
		else{
			schoolForm.setCityList(manager.selectAllCity(schoolForm.getParamCity()));
			return mapping.findForward("manageCity");
		}
	}
	
	public void setResult(boolean result, String action, HttpSession session, String task){
		String resultText = "";
		if(result){
			resultText = action + " data succeed.";
		}else{
			resultText = action + " data failed.";
		}
		session.setAttribute("result", resultText);
	}
}
